package com.silence.thread.pool;


import java.util.concurrent.*;

public class poolMain {
    public static void main(String[] args) throws Exception {

//         int corePoolSize
//         int maximumPoolSize
//         long keepAliveTime
//         TimeUnit unit
//         BlockingQueue<Runnable> workQueue,
//         ThreadFactory threadFactory,
//         RejectedExecutionHandler handler
        ExecutorService pool = new ThreadPoolExecutor(
                3,5,5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
                );

        // runnable
//        Runnable runnable1 = new MyRunnable_poor();
//        pool.execute(runnable1);
//        pool.execute(runnable1);
//        pool.execute(runnable1);
//        pool.execute(runnable1);
//        pool.execute(runnable1);
//        pool.execute(runnable1);
//        pool.execute(runnable1);
//        pool.execute(runnable1);
//        // 3个坑满 + 5任务队列，下一个开始创建临时线程
//        pool.execute(runnable1);
//        pool.execute(runnable1);
//        // 两个临时线程也在忙，该拒绝了
//        pool.execute(runnable1);
//        pool.shutdown();
        // callable
//        MyCallable_poor callable1 = new MyCallable_poor();
//        Future<Integer> f1 = pool.submit(callable1);
//        Future<Integer> f2 = pool.submit(callable1);
//        Future<Integer> f3 = pool.submit(callable1);
//        Future<Integer> f4 = pool.submit(callable1);
//        System.out.println(f1.get());
//        System.out.println(f2.get());
//        System.out.println(f3.get());
//        System.out.println(f4.get());
//        pool.shutdown();

        // executors
        Executors.newFixedThreadPool(3);
        Executors.newSingleThreadExecutor();
        Executors.newCachedThreadPool();
        Executors.newScheduledThreadPool(5);
    }
}
